package com.ruoyi.manage.controller;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.Smallarea;
import com.ruoyi.manage.service.ISmallareaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 小区 信息操作处理
 * 
 * @author KEXUE
 * @date 2019-04-18
 */
@Controller
@RequestMapping("/manage/smallarea")
public class SmallareaController extends BaseController
{
    private String prefix = "manage/smallarea";
	
	@Autowired
	private ISmallareaService smallareaService;
	
	@RequiresPermissions("manage:smallarea:view")
	@GetMapping()
	public String smallarea()
	{
	    return prefix + "/smallarea";
	}

	@RequiresPermissions("manage:smallarea:list")
	@GetMapping("/list")
	@ResponseBody
	public List<Smallarea> list(Smallarea smallarea)
	{
		List<Smallarea> smallareaList = smallareaService.selectSmallareaList(smallarea);
		return smallareaList;
	}

	/**
	 * 新增小区
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
	{
		mmap.put("smallarea", smallareaService.selectSmallareaById(parentId));
		return prefix + "/add";
	}

	/**
	 * 新增保存小区
	 */
	@Log(title = "小区管理", businessType = BusinessType.INSERT)
	@RequiresPermissions("manage:smallarea:add")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Smallarea smallarea)
	{
		smallarea.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(smallareaService.insertSmallarea(smallarea));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{smallareaId}")
	public String edit(@PathVariable("smallareaId") Long smallareaId, ModelMap mmap)
	{
		Smallarea smallarea = smallareaService.selectSmallareaById(smallareaId);
		if (StringUtils.isNotNull(smallarea) && 100L == smallareaId)
		{
			smallarea.setParentName("无");
		}
		mmap.put("smallarea", smallarea);
		return prefix + "/edit";
	}

	/**
	 * 保存
	 */
	@Log(title = "小区管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("manage:smallarea:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Smallarea smallarea)
	{
		smallarea.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(smallareaService.updateSmallarea(smallarea));
	}

	/**
	 * 删除
	 */
	@Log(title = "小区管理", businessType = BusinessType.DELETE)
	@RequiresPermissions("manage:smallarea:remove")
	@GetMapping("/remove/{smallareaId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("smallareaId") Long smallareaId)
	{
		if (smallareaService.selectSmallareaCount(smallareaId) > 0)
		{
			return AjaxResult.warn("存在下级小区,不允许删除");
		}
		if (smallareaService.checkSmallareaExistUser(smallareaId))
		{
			return AjaxResult.warn("小区存在用户,不允许删除");
		}
		return toAjax(smallareaService.deleteSmallareaById(smallareaId));
	}

	/**
	 * 校验小区名称
	 */
	@PostMapping("/checkSmallareaNameUnique")
	@ResponseBody
	public String checkSmallareaNameUnique(Smallarea smallarea)
	{
		return smallareaService.checkSmallareaNameUnique(smallarea);
	}

	/**
	 * 选择小区树
	 */
	@GetMapping("/selectSmallareaTree/{smallareaId}")
	public String selectSmallareaTree(@PathVariable("smallareaId") Long smallareaId, ModelMap mmap)
	{
		mmap.put("smallarea", smallareaService.selectSmallareaById(smallareaId));
		return prefix + "/tree";
	}

	/**
	 * 加载小区列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData()
	{
		List<Ztree> ztrees = smallareaService.selectSmallareaTree(new Smallarea());
		return ztrees;
	}

	/**
	 * 加载角色小区（数据权限）列表树
	 */
	@GetMapping("/roleSmallareaTreeData")
	@ResponseBody
	public List<Ztree> smallareaTreeData(SysRole role)
	{
		List<Ztree> ztrees = smallareaService.roleSmallareaTreeData(role);
		return ztrees;
	}
}
